//
//  PhotosViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/25.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "PhotosViewController.h"
#import "PhotoCollectionViewCell.h"
static NSString * CellIdentifer = @"PhotoCollectionViewCell";

@interface PhotosViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,MWPhotoBrowserDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIButton *headTitle;
@property (nonatomic,strong)  NSMutableArray  *photos;
@property(nonatomic,strong)MWPhotoBrowser * browser;
@end

@implementation PhotosViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _photos = [NSMutableArray array];
    [self initViewLayout];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 4*VerticalRatio();
    flowLayout.minimumLineSpacing = 4*VerticalRatio();
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.userInteractionEnabled=YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:CellIdentifer bundle:nil] forCellWithReuseIdentifier:CellIdentifer];
    
    if ([_photosType isEqualToString:@"1"]) {
        [_headTitle setTitle:@"商家相册" forState:UIControlStateNormal];
    }else{
        [_headTitle setTitle:@"剧照" forState:UIControlStateNormal];
    }
    
    
    _browser = [[MWPhotoBrowser alloc] init];
    
    // Set options
    _browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
    _browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
    _browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
    _browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
    _browser.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
    _browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
    _browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
    
    // Optionally set the current visible photo before displaying
    // Manipulate
    [_browser showNextPhotoAnimated:YES];
    [_browser showPreviousPhotoAnimated:YES];


}

- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}


#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
        return _imagesArray.count;

    
    
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

        return CGSizeMake(75*VerticalRatio(), 75*VerticalRatio());

    
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
        return UIEdgeInsetsMake(4*VerticalRatio(),4*VerticalRatio(), 0, 4*VerticalRatio());
    
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

        PhotoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifer forIndexPath:indexPath];
        

                NSDictionary * dic = _imagesArray[indexPath.row];
                NSString * image = dic[@"storeImg"];
                if (image.length>0) {
                    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image]];
                }
    

        
        return cell;
        
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [_photos removeAllObjects];
    for (int i = 0; i<_imagesArray.count; i++) {
        NSDictionary * dic = _imagesArray[i];
        NSString * image = dic[@"storeImg"];
        MWPhoto * photo = [[MWPhoto alloc]initWithURL:[NSURL URLWithString:image]];
        [_photos addObject:photo];
    }
    _browser.delegate = self;
    [_browser setCurrentPhotoIndex:indexPath.row];
    if ([_photosType isEqualToString:@"1"]) {
        _browser.titleStr = @"商家相册";
    }else{
        _browser.titleStr = @"剧照";
    }
    
    // Present
    MyNavigationViewController * navC = (MyNavigationViewController *)self.navigationController;
    [navC pushToViewController:_browser ];

}


- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    return nil;
}

- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index{
    NSString * str = [NSString stringWithFormat:@"%ld/%ld",index+1,_photos.count];
    return str;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
